package cn.itcast.bos.web.action.area;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.service.area.AreaService;
import cn.itcast.bos.utils.PinYin4jUtils;
import cn.itcast.bos.web.action.base.BaseAction;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
@Controller
@Namespace("/")
@ParentPackage("json-default")
@Scope("prototype")
public class AreaAction extends BaseAction<Area>{
	@Autowired
	private AreaService areaService;
	@Action(value="area_search",results={@Result(name="success",type="json")})
	public String pageQuery(){
		Pageable pageable= new PageRequest(page-1, rows);
		Specification<Area> specification= new Specification<Area>() {
			
			@Override
			public Predicate toPredicate(Root<Area> root, CriteriaQuery<?> query,
					CriteriaBuilder cb) {
				List<Predicate> list= new ArrayList<Predicate>();
				if(StringUtils.isNotBlank(model.getProvince())){
					Predicate p1=cb.like(root.get("province").as(String.class), "%"+model.getProvince()+"%");
					list.add(p1);
				}
				if(StringUtils.isNoneBlank(model.getCity())){
					Predicate p2=cb.like(root.get("city").as(String.class), "%"+model.getCity()+"%");
					list.add(p2);
				}
				if(StringUtils.isNotBlank(model.getDistrict())){
					Predicate p3=cb.like(root.get("district").as(String.class), "%"+model.getDistrict()+"%");
					list.add(p3);
				}		
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		Page page=areaService.findPageDate(specification,pageable);
		pushPageDataToValueStack(page);
		
		return "success";
	}
	
	
	
	
	// 文件的批量上传
	private File file;
	
	private String fileFileName;
	
	public void setFile(File file) {
		this.file = file;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}
	@Action(value="area_import")
	public String impot() throws Exception{
		List<Area>areas= new ArrayList<Area>();
		
		
		HSSFWorkbook hssfWorkBook= new HSSFWorkbook(new FileInputStream(file));
		HSSFSheet sheet = hssfWorkBook.getSheetAt(0);
	
		for(Row row: sheet){
			if(row.getRowNum()==0){
				continue;
			}
			// 这里判断的是值为空的时候!!!!! 
			//StringUtils.isBlank() 值为空
			//StringUtils.isNotBlank()值不为空
			if(row.getCell(0)==null||StringUtils.isBlank(row.getCell(0).getStringCellValue())){
				//row.getCell(0) 获取到第一个单元格对象(里面有很多的属性 格式 边框...)
				
				//row.getCell(0).getStringCellValue() 获取单元格里面的额内容
				continue;				
			}
			Area area= new Area();
			area.setId(row.getCell(0).getStringCellValue());
			area.setProvince(row.getCell(1).getStringCellValue());
			area.setCity(row.getCell(2).getStringCellValue());
			area.setDistrict(row.getCell(3).getStringCellValue());
			area.setPostcode(row.getCell(4).getStringCellValue());
			//pinyin4j 生成的代码和简码
			String province = area.getProvince();
			String city = area.getCity();
			String district = area.getDistrict();
			// 去掉最后一个子 
			province=province.substring(0, province.length()-1);
			city=city.substring(0,city.length()-1);
			district=district.substring(0,district.length()-1);
			//生成简码
			String [] headArray=PinYin4jUtils.getHeadByString(province+
					city+district);
			StringBuffer sb= new StringBuffer();
			for (String str : headArray) {
				sb.append(str);
			}
			String shortCode= sb.toString();
			area.setPostcode(shortCode);
			//生成城市的编码
			String cityCode= PinYin4jUtils.hanziToPinyin(city, "");
			area.setCitycode(cityCode);
			areas.add(area);			
		}
	
		areaService.saveBath(areas);
		
		
		return NONE;
	}
}













